package com.jzl.swing.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.jzl.swing.entity.Supplier;
import com.jzl.swing.service.SupplierService;
import com.jzl.swing.service.impl.SupplierServiceImpl;

import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ModifySupplierUI {

	private JFrame frame;
	private JTextField textFieldName;
	private JTextField textFieldContact;
	private JTextField textFieldPhone;
	private JTextField textFieldAddr;
	private SupplierService supplierService = new SupplierServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ModifySupplierUI window = new ModifySupplierUI(args[0]);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ModifySupplierUI(String supplier_id) {
		try {
			initialize(supplier_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws Exception 
	 */
	private void initialize(String supplier_id) throws Exception {
		Supplier supplier = supplierService.queryById(supplier_id);
		
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 543);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("供应商名称：");
		label.setBounds(75, 54, 83, 16);
		frame.getContentPane().add(label);
		
		textFieldName = new JTextField();
		textFieldName.setBounds(170, 49, 130, 26);
		frame.getContentPane().add(textFieldName);
		textFieldName.setColumns(10);
		textFieldName.setText(supplier.getSupplier_name());
		
		JLabel label_1 = new JLabel("供应商描述：");
		label_1.setBounds(75, 145, 83, 16);
		frame.getContentPane().add(label_1);
		
		JTextArea textAreaDesc = new JTextArea();
		textAreaDesc.setBounds(183, 132, 198, 96);
		frame.getContentPane().add(textAreaDesc);
		textAreaDesc.setText(supplier.getsupplier_describ());
		
		JLabel label_2 = new JLabel("供应商联系人：");
		label_2.setBounds(75, 267, 96, 16);
		frame.getContentPane().add(label_2);
		
		textFieldContact = new JTextField();
		textFieldContact.setBounds(183, 262, 130, 26);
		frame.getContentPane().add(textFieldContact);
		textFieldContact.setColumns(10);
		textFieldContact.setText(supplier.getsupplier_contacter());
		
		JLabel label_3 = new JLabel("供应商电话：");
		label_3.setBounds(75, 347, 83, 16);
		frame.getContentPane().add(label_3);
		
		textFieldPhone = new JTextField();
		textFieldPhone.setBounds(183, 337, 130, 26);
		frame.getContentPane().add(textFieldPhone);
		textFieldPhone.setColumns(10);
		textFieldPhone.setText(supplier.getsupplier_phone());
		
		JLabel label_4 = new JLabel("供应商地址：");
		label_4.setBounds(75, 416, 90, 16);
		frame.getContentPane().add(label_4);
		
		textFieldAddr = new JTextField();
		textFieldAddr.setBounds(183, 411, 130, 26);
		frame.getContentPane().add(textFieldAddr);
		textFieldAddr.setColumns(10);
		textFieldAddr.setText(supplier.getsupplier_address());
		
		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String supplier_name = textFieldName.getText();
				String supplier_describ = textAreaDesc.getText();
				String supplier_phone = textFieldPhone.getText();
				String supplier_addr = textFieldAddr.getText();
				String supplier_contacter = textFieldContact.getText();
				Supplier supplier = new Supplier();
				supplier.setSupplier_id(supplier_id);
				supplier.setSupplier_name(supplier_name);
				supplier.setsupplier_describ(supplier_describ);
				supplier.setsupplier_phone(supplier_phone);
				supplier.setsupplier_address(supplier_addr);
				supplier.setsupplier_contacter(supplier_contacter);
				try {
					int i = supplierService.modifySupplier(supplier);
					if (i>0) {
						JOptionPane.showMessageDialog(frame, "修改成功！");
						frame.dispose();
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button.setBounds(66, 473, 117, 29);
		frame.getContentPane().add(button);
		
		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(frame, "您填写的数据将不会被保存，是否退出？");
				if (i==0) {
					frame.dispose();
				}
			}
		});
		button_1.setBounds(253, 473, 117, 29);
		frame.getContentPane().add(button_1);
	}

}
